package gui;

import services.ProjectService;
import services.ProjectServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.awt.image.BufferedImage;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 19.04.2009
 * Time: 14:05:51
 * To change this template use File | Settings | File Templates.
 */
public class MainModulePage{
    private static int WIDTH = 500;
    private static int HEIGHT = 500;

    private JFrame frame;
    private JPanel leftPanel;
    private Image image;
    private JPanel imagePanel;
    private ProjectService projectService;

    public void init() {
        frame = new JFrame ("Graphic editor");
        projectService = new ProjectServiceImpl();
        frame.setSize(new Dimension(WIDTH, HEIGHT));
        frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        Container content = frame.getContentPane();
        imagePanel = new ImagePanel();
        imagePanel.setBackground(Color.green);

        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu("File");
        menuFile.setMnemonic(KeyEvent.VK_A);
        JMenuItem openItem = new JMenuItem(new AbstractAction("Open") {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                }
            }
        });
        menuFile.add(openItem);
        JMenu menuSettings = new JMenu("Settings");
        menuBar.add(menuFile);
        menuBar.add(menuSettings);
        frame.setJMenuBar(menuBar);

        leftPanel = new JPanel(new FlowLayout(0));
        JButton openButton = new JButton("Open");

        openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                    image = Toolkit.getDefaultToolkit().getImage(
                            fileChooser.getSelectedFile().toString());
                }
                ((ImagePanel)imagePanel).rightSize();
            }
        });
        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                projectService.saveImage(image);
            }
        });
        leftPanel.add(openButton);
        content.add(leftPanel, BorderLayout.WEST);

        JColorChooser colChooser = new JColorChooser(Color.BLACK);
        colChooser.setChooserPanels(colChooser.getChooserPanels());

        content.add(imagePanel, BorderLayout.CENTER);
        frame.setVisible(true);
        imagePanel.addNotify();
    }

    public class ImagePanel extends JPanel {
        public boolean imageUpdate(Image img, int infoflags,
                               int x, int y, int width, int height) {
            if ((infoflags & ImageObserver.ERROR) != 0) {
                System.out.println("Error loading image!");
                System.exit(-1);
            }
            if ((infoflags & ImageObserver.WIDTH) != 0 &&
                    (infoflags & ImageObserver.HEIGHT) != 0)
                rightSize();
            if ((infoflags & ImageObserver.SOMEBITS) != 0)
                imagePanel.repaint();
            if ((infoflags & ImageObserver.ALLBITS) != 0) {
                rightSize();
                imagePanel.repaint();
                return false;
            }
            return true;
        }

        public void paint(Graphics g) {
            Insets insets = imagePanel.getInsets();

//            Toolkit.getDefaultToolkit().prepareImage(image, 200, 200 ,imagePanel);

            if (image != null) {
                Image scaledImage = image.getScaledInstance(500, 500, 50);
                g.drawImage(scaledImage, insets.left, insets.top, imagePanel);
            }
        }

        public void update(Graphics g) {
            paint(g);
        }

        public void rightSize() {
            int width = image.getWidth(imagePanel);
            int height = image.getHeight(imagePanel);
            if (width == -1 || height == -1) return;
            imagePanel.addNotify();
            Insets insets = imagePanel.getInsets();
//            imagePanel.setSize(width + insets.left + insets.right,
//                    height + insets.top + insets.bottom);
//            frame.setSize(imagePanel.getSize());
            imagePanel.setVisible(true);
//            image.flush();
        }
    }
}
